package com.taichuan.selfcheck.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.g3288.g3288board.Rk3399GpioActionDefine;
import com.g3288.g3288board.TcRk3399GpioClient;
import com.taichuan.keysupport.Key;
import com.taichuan.mvp.fragment.BaseFragmentManager;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.config.WebUrlConfig;
import com.taichuan.selfcheck.entity.TestResultEntity;
import com.taichuan.selfcheck.mvp.presenter.MainPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.MainInterface;
import com.taichuan.selfcheck.ui.activity.MainActivity;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.tcutils.taichuan.SystemUtils;

import org.greenrobot.eventbus.EventBus;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2021/1/30
 */
public class MainFragment3 extends AppBaseFragment<MainInterface, MainPresenter> implements MainInterface {
    private static final String TAG = MainFragment3.class.getSimpleName();

    @BindView(R.id.check_result_button)
    Button check_result_button;
    @BindView(R.id.auto_check_button)
    Button auto_check_button;
    @BindView(R.id.public_ip_query_button)
    Button publicIpQueryBtn;
    @BindView(R.id.sd_card_and_usb_test_button)
    Button sdCardAndUsbTestBtn;

    @BindView(R.id.test_button)
    Button testBtn;

    private BaseFragmentManager activityBaseFragmentManager;
    private MainActivity mMainActivity;

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_main_fragment3;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMainActivity = (MainActivity) context;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //隐藏MainActivity 中的返回按钮
//            showOrHideMainBottomButton(false);
        } else {
            //显示MainActivity 中的返回按钮
            showTitle(R.string.settings_function_test);
        }
    }

    @OnClick({R.id.check_result_button, R.id.auto_check_button, R.id.public_ip_query_button, R.id.sd_card_and_usb_test_button,
            R.id.usb_device_list_test_button,R.id.test_button,R.id.device_mcu_update_button})
    @Override
    protected void onXmlClick(View v) {
        switch (v.getId()) {
            case R.id.public_ip_query_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.public_ip_query);
                    //打开WebViewFragment
                    BaseWebViewFragment webViewFragment = new BaseWebViewFragment();
                    webViewFragment.setArguments(BaseWebViewFragment.setBundleValue(WebUrlConfig.PUBLIC_IP_QUERY_WEB_URL));
                    activityBaseFragmentManager.replace(webViewFragment);
                }
                break;
            }
            case R.id.check_result_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.check_result);
                    activityBaseFragmentManager.replace(new TestResultFragment());
                }
                break;
            }

            case R.id.sd_card_and_usb_test_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.sd_card_and_usb_test);
                    activityBaseFragmentManager.replace(new TestSdCardAndUsbAndUsbFragment());
                }
                break;
            }
            case R.id.usb_device_list_test_button:{
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.usb_device_list_test);
                    activityBaseFragmentManager.replace(new TestUsbDeviceListFragment());
                }
                break;
            }

            case R.id.device_mcu_update_button:{
                if (SystemUtils.isSupportMcuUpdate()){
                    if (activityBaseFragmentManager != null) {
                        showTitle(R.string.device_mcu_update);
                        activityBaseFragmentManager.replace(new McuUpdateFragment());
                    }
                }else {
                    showMsg("该机型不支持单片机升级功能，无法使用");
                }
                break;
            }

            case R.id.auto_check_button: {
                EventBus.getDefault().post(new TestResultEntity(0, getResources().getString(R.string.auto_check), PropertyDefaultValue.FUNCTION_TEST_RESULT));
                break;
            }
//
//            case R.id.test_button:{
//                if (testBtn.getTag().equals("close")) {
//                    //开启 彩色摄像头
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_USB_COLOR_CAMERA_OPEN);
//
//                    //开启 红外摄像头
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_USB_INFRARED_CAMERA_OPEN);
//
//                    //开启 CPU风扇
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_CPU_FAN_OPEN);
//
//                    //开启 热敏打印机电源
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_PRINTER_POWER_OPEN);
//
//                    //开启 外接副板 电源指示灯
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_POWER_LED_OPEN);
//
//                    //开启 外接副板 以太网网络指示灯
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_ETHERNET_0_LED_OPEN);
//
//                    //开启 外接 灯板电源
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_LIGHT_BOARD_OPEN);
//
//                    showMsg("开启");
//                } else {
//                    //关闭 彩色摄像头
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_USB_COLOR_CAMERA_CLOSE);
//
//                    //关闭 红外摄像头
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_USB_INFRARED_CAMERA_CLOSE);
//
//                    //关闭 CPU风扇
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_CPU_FAN_CLOSE);
//
//                    //关闭 热敏打印机电源
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_PRINTER_POWER_CLOSE);
//
//                    //关闭 外接副板 电源指示灯
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_POWER_LED_CLOSE);
//
//                    //关闭 外接副板 以太网网络指示灯
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_ETHERNET_0_LED_CLOSE);
//
//                    //关闭 外接 灯板电源
//                    TcRk3399GpioClient.getInstance().controlGpio(Rk3399GpioActionDefine.CONTROL_LIGHT_BOARD_CLOSE);
//
//                    showMsg("关闭");
//                }
//                break;
//            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()) {
            case Key.Convert.KEYCODE_1:        //按键1——
                publicIpQueryBtn.performClick();
                break;
            case Key.Convert.KEYCODE_2:        //按键2——
                sdCardAndUsbTestBtn.performClick();
                break;
            case Key.Convert.KEYCODE_3:        //按键3——

                break;
            case Key.Convert.KEYCODE_4:        //按键4——
                break;
            case Key.Convert.KEYCODE_5:             //按键5——
                break;
            case Key.Convert.KEYCODE_6:             //按键6——
                break;
            case Key.Convert.KEYCODE_7:
                break;
            case Key.Convert.KEYCODE_8:
                break;
            case Key.Convert.KEYCODE_JING:     //按键# ——测试报告
                check_result_button.performClick();
                break;
            case Key.Convert.KEYCODE_LONG_JING:    //长按# ——自动测试
                EventBus.getDefault().post(new TestResultEntity(0, getResources().getString(R.string.auto_check), PropertyDefaultValue.FUNCTION_TEST_RESULT));
                break;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        activityBaseFragmentManager = getActivityBaseFragmentManager();

        testBtn.setTag("open");
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {

    }

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    /**
     * 定义一个方法用于 显示标题
     */
    private void showTitle(int title) {
        if (mMainActivity != null) {
//            if (mMainActivity.isStartAppByThree()){
//
//                mMainActivity.showToolBarTitle(title);
//            }
            mMainActivity.showToolBarTitle(title);
        }
    }
}
